package com.test.controller;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.JSONToken;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.test.model.Users;

import java.util.List;

@Path("/login")
public class LoginController extends Controller {

    @Clear
    public void index(){
        render("login.html");
    }

    @Clear
    public void doLogin(){
        String username=getPara("username");
        String password=getPara("password");
        List<Users> list = Users.dao.find("select * from sys_users where user_name=? and pass_word=?",username,password);
        if(list.size()>0){
            setSessionAttr("user",list.get(0));
            //登录成功
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","登陆成功");
            renderJson(jsonObject);

        }else{
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("code",-1);
            jsonObject.put("msg","登陆失败，用户名或者密码错误");
            renderJson(jsonObject);
        }
    }
}
